﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCQuick.Framework.Container;

namespace MVCQuick.Framework.Mvc
{
    /// <summary>
    /// ActionInvoker implementation that enables the <see cref="IApplicationContext"/>to satisfy dependencies on ActionFilter attributes.
    /// </summary>
    public class ClassicActionInvoker : ControllerActionInvoker
    {
     
        /// <summary>
        /// 
        /// </summary>
        public ClassicActionInvoker()
        {
            
        }

        /// <summary>
        /// Retrieves information about the action filters.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <param name="actionDescriptor">The action descriptor.</param>
        /// <returns>Information about the action filters.</returns>
        protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            //let the base class do the actual work as usual
            var filterInfo = base.GetFilters(controllerContext, actionDescriptor);

            //configure each collection of filters using the IApplicationContext
            foreach (IActionFilter filter in filterInfo.ActionFilters.Where(f => f != null))
            {
                ClassicContainer.ConfigureObject(filter, filter.GetType().FullName);
            }

            foreach (IAuthorizationFilter filter in filterInfo.AuthorizationFilters.Where(f => f != null))
            {
                ClassicContainer.ConfigureObject(filter, filter.GetType().FullName);
            }

            foreach (IExceptionFilter filter in filterInfo.ExceptionFilters.Where(f => f != null))
            {
                ClassicContainer.ConfigureObject(filter, filter.GetType().FullName);
            }

            foreach (IResultFilter filter in filterInfo.ResultFilters.Where(f => f != null))
            {
                ClassicContainer.ConfigureObject(filter, filter.GetType().FullName);
            }

            return filterInfo;
        }

    }
}